package com.yunxi.controller.vip;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.vip.pojo.VipApplyVO;
import com.yunxi.vip.pojo.VipVO;
import com.yunxi.vip.service.VipApplyDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2019-09-06
 */
@RestController
@RequestMapping("/admin/vipapply")
@Api(tags = "pms:vipapply", description = "接口")
public class VipApplyController {
 private static final Logger logger = LoggerFactory.getLogger(VipApplyController.class);

 @Autowired
 VipApplyDubboService vipApplyDubboService;

 @GetMapping("/page")
 @ApiOperation(value = "大客户申请查询")
 public ApiResult page(VipApplyVO vipApplyVO, Integer pageNum, Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {

     if(StringUtils.isNotEmpty(loginInfo.getPartnerCode())){
         vipApplyVO.setPartnerCode(loginInfo.getPartnerCode());
         vipApplyVO.setSubmitType(0);
     }else{
         vipApplyVO.setPartnerCodeNot("1");
         vipApplyVO.setSubmitType(1);
     }

  return ResponseUtil.ok(vipApplyDubboService.findPageByVO(vipApplyVO,pageNum,pageSize));
 }

@GetMapping("/list")
   @ApiOperation(value = "查询列表")
   public ApiResult list(VipApplyVO vipApplyVO) throws Exception {
      return ResponseUtil.ok(vipApplyDubboService.listByVo(vipApplyVO));
   }

 @GetMapping("/load")
 @ApiOperation(value = "读取单个")
 public ApiResult load(VipApplyVO vipApplyVO, @CurrentUser LoginInfo loginInfo) throws Exception {
 return ResponseUtil.ok(vipApplyDubboService.getByCas(vipApplyVO.getApplyId()));
}

 @PostMapping("/save")
 @ApiOperation(value = "保存信息")
 public ApiResult save(@RequestBody VipApplyVO vipApplyVO,@CurrentUser LoginInfo loginInfo) throws Exception {
     vipApplyVO.setOp("first");
    vipApplyDubboService.saveCas(vipApplyVO,loginInfo);
  return ResponseUtil.ok("success");
}

 @PostMapping("/del")
 @ApiOperation(value = "删除")
 public ApiResult del(@RequestBody List<VipApplyVO> vipApplyVOList) throws Exception {
    vipApplyDubboService.removeByListVo(vipApplyVOList);
 return ResponseUtil.ok("success");
 }

    @PostMapping("/delById")
    @ApiOperation(value = "删除")
    public ApiResult delById(@RequestBody VipApplyVO vipApplyVO) throws Exception {
        vipApplyDubboService.removeByVoId(vipApplyVO);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/apply")
    @ApiOperation(value = "申請")
    public ApiResult apply(@RequestBody VipApplyVO vipApplyVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        vipApplyVO.setOp("first");
        return vipApplyDubboService.apply(vipApplyVO,loginInfo);
    }

    @PostMapping("/saveAttach")
    @ApiOperation(value = "删除体验车库存")
    public ApiResult saveAttach(@RequestBody VipApplyVO vipApplyVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        vipApplyDubboService.saveAttach(vipApplyVO,loginInfo);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/next")
    @ApiOperation(value = "审批下一步")
    public ApiResult next(@RequestBody VipApplyVO vipApplyVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        //if(vipApplyVO.getDiscount()!=null)
        //vipApplyDubboService.updateDiscount(vipApplyVO);
        vipApplyVO.setOp("update");
        vipApplyDubboService.saveCas(vipApplyVO,loginInfo);
        vipApplyDubboService.next(vipApplyVO,loginInfo);
        return ResponseUtil.ok("success");
    }

    @GetMapping("/pageWait")
    @ApiOperation(value = "待办")
    public ApiResult pageWait(VipApplyVO vipApplyVO,Integer pageNum,Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
        vipApplyVO.setRoleCode(loginInfo.getRoleCode());
        return ResponseUtil.ok(vipApplyDubboService.pageWait(vipApplyVO,pageNum,pageSize,loginInfo));
    }

    @GetMapping("/pageDone")
    @ApiOperation(value = "已办")
    public ApiResult pageDone(VipApplyVO vipApplyVO,Integer pageNum,Integer pageSize,@CurrentUser LoginInfo loginInfo) throws Exception {
        vipApplyVO.setAuditUserId(loginInfo.getUserCode());
        return ResponseUtil.ok(vipApplyDubboService.pageDone(vipApplyVO,pageNum,pageSize,loginInfo));
    }

    @PostMapping("/b2b")
    @ApiOperation(value = "审批生成")
    public ApiResult b2b(@RequestBody VipApplyVO vipApplyVO,@CurrentUser LoginInfo loginInfo) throws Exception {
        vipApplyDubboService.b2b(vipApplyVO,loginInfo);
        return ResponseUtil.ok("success");
    }

    public ApiResult getVipByOrderCustomerNo(String orderCustomerNo){
        VipVO vip = vipApplyDubboService.getVipByOrderCustomerNo(orderCustomerNo);
        return ResponseUtil.ok(vip);
    }
}
